using EnvDTE;
namespace Dslfactory.wcfLanguage
{
 /// <summary>
 /// Reverse engineering of data classes
 /// </summary>
 internal static class DataClassReverseEngineering
 {
  /// <summary>
  /// Update the domain properties of the data class from the codetype implemented it
  /// </summary>
  public static void UpdateDataClassProperties(DataClass dataClass, CodeType codeType)
  {
   dataClass.Name = codeType.Name;
   dataClass.FullName = codeType.FullName;
   dataClass.Namespace = codeType.Namespace.FullName;
  }

  /// <summary>
  /// Update the properties of a field or property from its implementation
  /// </summary>
  public static void UpdateFieldOrProperty(FieldOrProperty member, CodeElement e)
  {
   member.Name = e.Name;
   if (e.Kind == vsCMElement.vsCMElementVariable)
   {
    member.Kind = DataMemberKind.Field;
    member.MemberFullTypeName = (e as CodeVariable).Type.AsString;
    member.Visibility = GetVisibilityFromAccess((e as CodeVariable).Access);
   }
   else if (e.Kind == vsCMElement.vsCMElementProperty)
   {
    member.Kind = DataMemberKind.Property;
    member.MemberFullTypeName = (e as CodeProperty).Type.AsString;
    member.Visibility = GetVisibilityFromAccess((e as CodeProperty).Access);
   }
  }


  /// <summary>
  /// Get the visibility from the Visual Studio Access enum value
  /// </summary>
  /// <param name="access"></param>
  /// <returns></returns>
  private static Visibility GetVisibilityFromAccess(vsCMAccess access)
  {
   switch (access)
   {
    case vsCMAccess.vsCMAccessAssemblyOrFamily:
     return Visibility.Internal;

    case vsCMAccess.vsCMAccessDefault:
     return Visibility.Internal;

    case vsCMAccess.vsCMAccessPrivate:
     return Visibility.Private;

    case vsCMAccess.vsCMAccessProject:
     return Visibility.Internal;
     
    case vsCMAccess.vsCMAccessProjectOrProtected:
     return Visibility.Internal;
     
    case vsCMAccess.vsCMAccessProtected:
     return Visibility.Protected;
     
    case vsCMAccess.vsCMAccessPublic:
     return Visibility.Public;
     
    case vsCMAccess.vsCMAccessWithEvents:
     return Visibility.Public;
     
    default:
     return Visibility.Private;
   }
  }
 }
}
